﻿namespace DreamBuilder.UI.UserControls
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using DreamBuilder.Common;
    using DreamBuilder.UI.UserControls.DataAccess.Services;
    using DreamBuilder.Core.Container;

    public delegate void RBLSelectHandler(object sender, System.EventArgs e);
    /// <summary>
    ///		RBList 的摘要说明。
    /// </summary>
    public partial class RBLReference : ControlBase
    {
        public event RBLSelectHandler RBLSelected;
        private string tableCode = ""; //参照表代码
        private string refCodeValue = ""; //参照代码	
        private string whereStr = "";//条件
        Unit width = new Unit("120px");
        private int repeatColumns = 1; //控件占用列数
        /// <summary>
        /// 控件长度
        /// </summary>
        public Unit Width
        {
            set
            {
                width = value;
                this.rbList.Width = value;
            }
            get
            {
                return width;
            }
        }

        /// <summary>
        /// 控件是否回传
        /// </summary>
        public bool AutoPostBack
        {
            set
            {
                this.rbList.AutoPostBack = value;
            }
            get
            {
                return this.rbList.AutoPostBack;
            }
        }


        /// <summary>
        /// 控件是否显示
        /// </summary>
        public bool Show
        {
            set
            {
                this.rbList.Visible = value;
            }
            get
            {
                return this.rbList.Visible;
            }
        }

        /// <summary>
        /// 控件是否运行编辑
        /// </summary>
        public bool Enable
        {
            set
            {
                this.rbList.Enabled = value;
            }
            get
            {
                return this.rbList.Enabled;
            }
        }
        //选择值
        public string Value
        {
            get
            {
                ViewState["Value"] = rbList.SelectedValue;
                return ViewState["Value"].ToString();
            }
            set
            {
                ViewState["Value"] = value;
            }
        }
        /// <summary>
        ///参照表的代码
        /// </summary>
        public string TableCode
        {
            set
            {
                tableCode = value;
                ViewState["RefTableCode"] = tableCode;
            }
            get
            {
                return tableCode;
            }
        }

        /// <summary>
        ///控件占用列数
        /// </summary>
        public int RepeatColumns
        {
            set
            {
                repeatColumns = value;
            }
            get
            {
                return repeatColumns;
            }
        }

        /// <summary>
        /// 条件
        /// </summary>
        public string WhereSql
        {
            set
            {
                whereStr = value;
            }
            get
            {
                return whereStr;
            }
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
        }
        //刷新
        public void Refresh()
        {
            this.Bind(ViewState["RefTableCode"].ToString());
        }
        //绑定数据
        private void Bind(string tableCode)
        {
            //设置选择控件为单列形式
            rbList.RepeatDirection = RepeatDirection.Horizontal;
            rbList.Items.Clear();
            string selectedValue = rbList.SelectedValue;
            if (string.IsNullOrEmpty(tableCode))
            {
                throw new ArgumentNullException("sTableCode");
            }
            UCControlCommon uCControlCommon = UContainer.Resolve<UCControlCommon>();
            System.Data.IDataReader drInfo = uCControlCommon.GetBindData(tableCode, whereStr);
            rbList.Items.Clear();
            while (drInfo.Read())
            {
                ListItem listItemCode = new ListItem(drInfo.GetValue(1).ToString(), drInfo.GetValue(0).ToString());
                rbList.Items.Add(listItemCode);
            }
            drInfo.Close();

            if (ViewState["Value"] != null)
            {
                rbList.SelectedValue = ViewState["Value"].ToString();
            }
            else //无缺省项选中第一项
            {
                if(rbList.Items.Count > 0)
                {
                    rbList.SelectedValue = rbList.Items[0].Value;
                }
            }
        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
            ControlPath = "UserControls/RBLReference";
            this.Bind(ViewState["RefTableCode"].ToString());
        }

        /// <summary>
        ///		设计器支持所需的方法 - 不要使用代码编辑器
        ///		修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {

        }
        #endregion

        protected void rbList_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (RBLSelected != null)
            {
                RBLSelected(this, e);
            }

        }

    }
}